package vcp.client.GUI.mvc.controller;

import javax.swing.SwingUtilities;

import vcp.client.GUI.events.FormSubmitionEvent;
import vcp.client.GUI.mvc.model.UIAbstractModel;
import vcp.client.GUI.mvc.model.UIApproveCostsModel;
import vcp.client.GUI.mvc.view.UIAbstractView;
import vcp.client.GUI.mvc.view.UIApproveCostsView;
import vcp.client.forms.ApproveCostForm;
import vcp.client.interfaces.listeners.IFormSubmitionListener;
import vcp.client.utility.Common.UserNotification;
import vcp.entity.Worker;
import vcp.enums.PopUpMessageType;
import vcp.enums.Screen;

/**
 * 
 * @author VCP team
 *         <p>
 *         coast approve controller
 *         </p>
 * 
 * @see UIApproveCostsView
 * @see UIApproveCostsModel
 * @see IFormSubmitionListener
 */
public class UIApproveCostsController extends UIAbstractController implements IFormSubmitionListener <ApproveCostForm>
{
	/** reference to the view **/
	private final UIApproveCostsView view;
	/** reference to model **/
	private final UIApproveCostsModel model;

	/**
	 * 
	 * @param view
	 *            new screen
	 * @param model
	 *            fill with info
	 */
	public UIApproveCostsController ( UIApproveCostsView view, UIApproveCostsModel model )
	{
		super(Screen.APPROVE_COSTS);
		this.view = view;
		this.model = model;
		this.setupListeners();
	}

	// ****************************************************************************************************************************
	// UIAbstractController methods
	// ****************************************************************************************************************************
	@Override
	public UIAbstractView getView ()
	{
		return this.view;
	}

	@Override
	public UIAbstractModel getModel ()
	{
		return this.model;
	}

	@Override
	public void clearMVC ()
	{
		this.view.setOnScreen(false);
	}

	@Override
	public void flashData ()
	{
		this.view.clearView();
		this.model.clearModel();
	}

	@Override
	public void uploadDataContext ( Object data )
	{
		if ( data instanceof Worker )
		{
			this.flashData();
			Worker worker = (Worker) data;

			this.model.setCEO(worker);
			this.reloadGUI();

			this.model.shouldRegisterForUpdates();
			this.view.setOnScreen(true);
		}
	}

	@Override
	public void updateControllerDelegateWithData ( Object newData )
	{
		this.parentDelegate.notifyOnDataUpdate(this.model.getCEO());
		if ( this.view.isOnScreen() )
		{
			this.parentDelegate.showPopUpMessage(String.format(UserNotification.__VALUE__IS_UPDATED, "Approve list"), PopUpMessageType.INFORMATION);
		}
		SwingUtilities.invokeLater(new Runnable()
			{
				@Override
				public void run ()
				{
					UIApproveCostsController.this.reloadGUI();
				}
			});
	}

	@Override
	protected void setupListeners ()
	{
		super.setupListeners();
		this.view.setFormSubmitionListener(this);
	}

	@Override
	protected synchronized void reloadGUI ()
	{
		this.view.loadPendingSiteCostsUpdates(this.model.getPendingSiteCostsUpdates());
	}

	// ****************************************************************************************************************************
	// IFormSubmitionListener methods
	// ****************************************************************************************************************************
	@Override
	public void onFormSubmitted ( FormSubmitionEvent <ApproveCostForm> event )
	{
		this.model.applyApprovalCosts(event.getForm());
	}
}
